﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InstaGM
{
    public partial class instagm_settings : Form
    {

        Form1 frm = new Form1();

        public instagm_settings()
        {
            InitializeComponent();
            this.items_to_itembox.Click += warn_user;
            ToolTip tooltip = new ToolTip();
            tooltip.ToolTipIcon = ToolTipIcon.Info;
            tooltip.ToolTipTitle = "Information";
            tooltip.IsBalloon = true;
            tooltip.ShowAlways = true;
            tooltip.SetToolTip(sa_q, "sa stands for \"System Administrator\"\n\nUse of \"sa\" IS NOT RECOMMENDED!!!\n\nExcepting only when using InstaGM to connect to your LOCAL database.");
            tooltip.SetToolTip(sa_q2, "sa stands for \"System Administrator\"\n\nUse of \"sa\" IS NOT RECOMMENDED!!!\n\nExcepting only when using InstaGM to connect to your LOCAL database.");
            tooltip.SetToolTip(connect_at_start_q, "Checking this option will tell InstaGM that you want to connect to Telnet immediately after it's launched");
            tooltip.SetToolTip(to_itembox_q, "Checking this option will tell InstaGM that you want Items to be sent to selected players Item Box.");
            tooltip.SetToolTip(to_clipboard_q, "Checking this option will tell InstaGM that you want Items to be sent to your Clip Board.");
            tooltip.SetToolTip(host_q, "If you're using InstaGM locally you can use localhost as your host, if your doing so remotely it is recommended to use the target machines IP.");
            tooltip.SetToolTip(always_to_itembox_q, "Checking this option will tell InstaGM you always want Summons/Items sent to the selected players Item Box");
            tooltip.SetToolTip(always_to_clipboard_q, "Checking this option will tell InstaGM you always want Summons/Items sent to your Clip Board");
            tooltip.SetToolTip(to_telnet_q, "Checking this option will tell InstaGM you want States to be sent via Telnet");
            tooltip.SetToolTip(state_to_clipboard_q, "Checking this option will tell InstaGM you want Buffs to be sent to your Clip Board");
        }

        private void warn_user(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.item_to_itembox_warning == false)
            {
                if (MessageBox.Show("By checking this box you acknowledge that InstaGM will connect to your database using the above connection information!\n\n If you do not want this, press CANCEL!\n\n NOTICE: It is recommended to follow the below guide before using this feature: http://goo.gl/z4sm0 \n\nTealSky is not responsible for improper security precations and or your giving this tool to people you don't absolutely trust!!!", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
                {
                    items_to_itembox.Checked = true;
                    Properties.Settings.Default.item_to_itembox = true;
                    Properties.Settings.Default.item_to_itembox_warning = true;
                    Properties.Settings.Default.Save();

                    if (items_to_clipboard.Checked == true)
                    {
                        items_to_clipboard.Checked = false;
                    }
                }
                else
                {
                    items_to_itembox.Checked = false;
                    Properties.Settings.Default.item_to_itembox = false;
                    Properties.Settings.Default.Save();
                }
            }
        }

        private void instagm_settings_load(object sender, EventArgs e)
        {
            host.Text = Properties.Settings.Default.host;
            port.Text = Properties.Settings.Default.port.ToString();
            pass.Text = Properties.Settings.Default.pass;
            db_name.Text = Properties.Settings.Default.db_name;
            db_user.Text = Properties.Settings.Default.db_user;
            db_pass.Text = Properties.Settings.Default.db_pass;
            auto_conn.Checked = Properties.Settings.Default.auto_conn;
            auto_tamed.Checked = Properties.Settings.Default.auto_tamed;
            always_to_itembox.Checked = Properties.Settings.Default.always_to_itembox;
            always_to_clipboard.Checked = Properties.Settings.Default.always_to_clipboard;
            items_to_itembox.Checked = Properties.Settings.Default.item_to_itembox;
            items_to_clipboard.Checked = Properties.Settings.Default.item_to_clipboard;
            to_telnet.Checked = Properties.Settings.Default.to_telnet;
            state_to_clipboard.Checked = Properties.Settings.Default.state_to_clipboard;
            add_control_event();
        }

        private void add_control_event()
        {
            always_to_itembox.Click += alter_from_always_item;
            always_to_clipboard.Click += alter_from_always_clipboard;
            items_to_itembox.Click += alter_iSettings; ;
            items_to_clipboard.Click += alter_cSettings;
        }

        public void alter_from_always_item(object sender, EventArgs e)
        {
            //If always_to_clip is already checked
            if (always_to_clipboard.Checked == true)
            {
                always_to_clipboard.Checked = false;
                items_to_clipboard.Checked = false;
                state_to_clipboard.Checked = false;
            }

            items_to_clipboard.Checked = false;

            if (always_to_itembox.Checked == true)
            {
                always_to_itembox.Checked = true;
                items_to_itembox.Checked = true;
            }
            else
            {
                always_to_itembox.Checked = false;
                items_to_itembox.Checked = false;
            }
        }

        public void alter_iSettings(object sender, EventArgs e)
        {
            if (always_to_clipboard.Checked == true)
            {
                always_to_clipboard.Checked = false;
                items_to_clipboard.Checked = false;
                state_to_clipboard.Checked = false;
            }
        }


        public void alter_from_always_clipboard(object sender, EventArgs e)
        {
            //If always_to_item is already checked
            if (always_to_itembox.Checked == true)
            {
                always_to_itembox.Checked = false;
                items_to_itembox.Checked = false;
            }

            items_to_itembox.Checked = false;

            if (always_to_clipboard.Checked == true)
            {
                always_to_clipboard.Checked = true;
                items_to_clipboard.Checked = true;
                state_to_clipboard.Checked = true;
            }
            else
            {
                always_to_clipboard.Checked = false;
                items_to_clipboard.Checked = false;
                state_to_clipboard.Checked = false;
            }
        }

        public void alter_cSettings(object sender, EventArgs e)
        {
            if (always_to_itembox.Checked == true)
            {
                always_to_itembox.Checked = false;
                items_to_itembox.Checked = false;
            }
            items_to_itembox.Checked = false;
            items_to_clipboard.Checked = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.host = host.Text;
            Properties.Settings.Default.port = Convert.ToInt32(port.Text);
            Properties.Settings.Default.pass = pass.Text;
            Properties.Settings.Default.db_name = db_name.Text;
            Properties.Settings.Default.db_user = db_user.Text;
            Properties.Settings.Default.db_pass = db_pass.Text;
            if (auto_conn.Checked == true)
            {
                Properties.Settings.Default.auto_conn = true;
            }
            else
            {
                Properties.Settings.Default.auto_conn = false;
            }
            if (auto_tamed.Checked == true)
            {
                Properties.Settings.Default.auto_tamed = true;
            }
            else 
            {
                Properties.Settings.Default.auto_tamed = false;
            }
            if (always_to_itembox.Checked == true)
            {
                Properties.Settings.Default.always_to_itembox = true;
                Properties.Settings.Default.item_to_itembox = true;
            }
            else
            {
                Properties.Settings.Default.always_to_itembox = false;
            }
            if (always_to_clipboard.Checked == true)
            {
                Properties.Settings.Default.always_to_clipboard = true;
                Properties.Settings.Default.item_to_clipboard = true;
                Properties.Settings.Default.state_to_clipboard = true;
            }
            else
            {
                Properties.Settings.Default.always_to_clipboard = false;
            }
            if (items_to_itembox.Checked == true)
            {
                Properties.Settings.Default.item_to_itembox = true;
            }
            else
            {
                Properties.Settings.Default.item_to_itembox = false;
            }
            if (items_to_clipboard.Checked == true)
            {
                Properties.Settings.Default.item_to_clipboard = true;
            }
            else
            {
                Properties.Settings.Default.item_to_clipboard = false;
            }
            if (to_telnet.Checked == true)
            {
                Properties.Settings.Default.to_telnet = true;
            }
            else
            {
                Properties.Settings.Default.to_telnet = false;
            }
            if (state_to_clipboard.Checked == true)
            {
                Properties.Settings.Default.state_to_clipboard = true;
            }
            else
            {
                Properties.Settings.Default.state_to_clipboard = false;
            }
            if (items_to_itembox.Checked == true && items_to_clipboard.Checked == true)
            {
                MessageBox.Show("You can not select both: To Item Box and To Clip Board, please choose one or the other!");
            }
            else
            {
                Properties.Settings.Default.Save();
                Application.Restart();
            }
        }
    }
}
